<?php
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Input;
class BecomeLawyerController extends CenterController {
    
    public function __construct()
    {
    	parent::__construct();
    }

    /**
     * 普通用户申请成为律师
     */
    public function becomelawyer() {

        if (Input::server ( 'REQUEST_METHOD' ) == 'POST') {
            $this->user->load('lawyer');
            $userArr = $this->user->toArray();
            // the given data did not pass validation
            $validLawyer = true;
            $required = array(
                'province', 
                'city', 
                'district', 
                'realname',
                'lawyer' => array(
                    'license', 
                    'license_file', 
                    'company_name', 
                    'practice_period',
                    'ID_img',
                    'text_reply_fee',
                    'phone_reply_fee',
                    'work_scope'
                )
            );
            $missingFields = [];
            foreach ($required as $key => $fields) {
                if (is_array($fields)) {
                    foreach ($fields as $value) {
                        if (!isset($userArr[$key][$value])) {
                            $missingFields[] = $value;
                        }
                    }
                } else {
                    if (!isset($userArr[$fields])) {
                        $missingFields[] = $value;
                        break;
                    }
                }
            }
            if (sizeof($missingFields)) {
                return $this->makeView(
                    'errors.400',
                    sprintf('请填写完整个人信息: %s', implode(",", $missingFields)),
                    400
                );
            }
            
            // save
            try {
                $user = User::becomelawyer($this->user->id);
                $user->load('lawyer');
            } catch(Exception $ex) {
                Log::error($ex);
                return $this->makeView(
                    'errors.503', 
                    '成为律师失败，请稍后再试。',
                    503
                );
            }
        }

        return $this->makeView('center.becomelawyer', [], 200);
    }

    public function lawyerInfoChecks() {

        $checks = [];
        $checks['work'] = false;
        $checks['identity'] = false;
        $checks['fee'] = false;
        $checks['workscope'] = false;
        $checks['experience'] = false;

        $this->user->load('lawyer');
        $userArr = $this->user->toArray();
        $lawyerArr = $userArr['lawyer'];
        $required = array(
            'province', 
            'city', 
            'district', 
            'realname',
            'lawyer' => array(
                'license', 
                'license_file', 
                'company_name', 
                'practice_period',
                'ID_img',
                'text_reply_fee',
                'phone_reply_fee',
                'work_scope'
            )
        );
        
        // work info
        if (
            isset($userArr['province']) && 
            isset($userArr['city']) && 
            isset($userArr['district']) && 
            isset($lawyerArr['license']) && 
            isset($lawyerArr['license_file']) && 
            isset($lawyerArr['company_name']) && 
            isset($lawyerArr['practice_period'])) {
            $checks['work'] = true;
        }

        // identity
        if (isset($userArr['realname']) && 
            isset($lawyerArr['ID_img'])) {
            $checks['identity'] = true;
        }

        // fee
        if ($lawyerArr['text_reply_fee'] != 0 && 
            $lawyerArr['phone_reply_fee'] != 0) {
            $checks['fee'] = true;
        }

        // workscope
        if (isset($lawyerArr['work_scope'])) {
            $checks['workscope'] = true;
        }

        // experience
        if (isset($lawyerArr['introduce']) && 
            isset($lawyerArr['experience']) && 
            isset($lawyerArr['law_cases'])) {
            $checks['experience'] = true;
        }

        return $this->makeView('center.becomelawyer.infochecks', $checks, 200);
    }

    /**
     * license
     * license_file
     * ompany_name
     * practice_period
     * address
     * 
     * 提交成为律师需要的从业信息
     */
    public function lawyerWorkInfo() {
        $this->user->load('lawyer');
        $userArr = $this->user->toArray();

        if (Input::server ( 'REQUEST_METHOD' ) == 'POST') {
            $rules = [];

            if (!$userArr['province']) {
                $rules['province'] = 'required';
            }
            if (!$userArr['city']) {
                $rules['city'] = 'required';
            }
            if (!$userArr['district']) {
                $rules['district'] = 'required';
            }
            if (!$userArr['lawyer']['license']) {
                $rules['license'] = 'required';
            }
            if (!$userArr['lawyer']['license_file']) {
                $rules['license_file'] = 'required';
            }
            if (!$userArr['lawyer']['company_name']) {
                $rules['company_name'] = 'required';
            }
            if (!$userArr['lawyer']['practice_period']) {
                $rules['practice_period'] = array('required', 'regex:/^(1to3|3to5|5to10|10)$/i');
            }
            
            $messages = array(
                'province.required' => '请选择省份',
                'city.required' => '请选择城市',
                'district.required' => '请选择地区',
                'license.required' => '请填写律师证号',
                'license_file.required' => '请上传律师证',
                'company_name.required' => '请填写律师事务所得名字',
                'practice_period.required' => '请选择律师的执业年限',
                'practice_period.regex' => '律师的执业年限值不正确',
            );

            $posts = Input::all();
            $validator = Validator::make ( $posts, $rules, $messages );
            if ($validator->fails()) {
                // the given data did not pass validation
                return $this->makeView(
                    'errors.400', 
                    $validator->errors(),
                    400
                );
            }
            
            // save
            try {
                $user = User::addOrUpdateLawyerWorkInfo($this->user->id, $posts);
            } catch(Exception $ex) {
                Log::error($ex);
                return $this->makeView(
                    'errors.503', 
                    '成为律师失败，请稍后再试。',
                    503
                );
            }
        } else {
            $user = $this->user;
        }

        $userArr = [];
        $userArr['id'] = $user['id'];
        $userArr['license'] = $user['lawyer']['license'];
        if (!$user['lawyer']['license_file']) {
            $userArr['license_file'] = "";
        } else {
            $userArr['license_file'] = UtilsHelper::getImage($user['lawyer']['license_file'], "");
        }
        $userArr['company_name'] = $user['lawyer']['company_name'];
        $userArr['province'] = AreaHelper::areaCodeToName($user['province']);
        $userArr['city'] = AreaHelper::areaCodeToName($user['city']);
        $userArr['district'] = AreaHelper::areaCodeToName($user['district']);
        $userArr['practice_period'] = $user['lawyer']['practice_period'];

        return $this->makeView('center.becomelawyer.work', $userArr, 200);
    }

    /**
     * realname
     * ID_img
     * 
     * 提交成为律师需要的实名认证信息
     */
    public function lawyerIdentityInfo() {
        $this->user->load('lawyer');
        $userArr = $this->user->toArray();

        if (Input::server ( 'REQUEST_METHOD' ) == 'POST') {
            $rules = [];

            if (!$userArr['realname']) {
                $rules['realname'] = 'required';
            }
            if (!$userArr['lawyer']['ID_img']) {
                $rules['ID_img'] = 'required';
            }

            $messages = array(
                'realname.required' => '请填写证实姓名',
                'ID_img.required' => '请上传身份证',
            );

            $posts = Input::all();
            $validator = Validator::make ( $posts, $rules, $messages );
            if ($validator->fails()) {
                // the given data did not pass validation
                return $this->makeView(
                    'errors.400', 
                    $validator->errors(),
                    400
                );
            }
            
            // save
            try {
                $user = User::addOrUpdateLawyerIdentityInfo($this->user->id, $posts);
            } catch(Exception $ex) {
                Log::error($ex);
                return $this->makeView(
                    'errors.503', 
                    '成为律师失败，请稍后再试。',
                    503
                );
            }
        } else {
            $user = $this->user;
        }

        $userArr = [];
        $userArr['id'] = $user['id'];
        $userArr['realname'] = $user['realname'];
        if (!$user['lawyer']['ID_img']) {
            $userArr['ID_img'] = "";
        } else {
            $userArr['ID_img'] = UtilsHelper::getImage($user['lawyer']['ID_img'], "img/dfimg.png");
        }

        return $this->makeView('center.becomelawyer.identity', $userArr, 200);
    }

    /**
     * text_reply_fee
     * phone_reply_fee
     * 
     * 提交成为律师需要的资费信息
     */
    public function lawyerFeeInfo() {
        $this->user->load('lawyer');
        $userArr = $this->user->toArray();

        if (Input::server ( 'REQUEST_METHOD' ) == 'POST') {
            $rules = [];

            if ($userArr['lawyer']['text_reply_fee']) {
                $rules['text_reply_fee'] = 'required';
            }
            if ($userArr['lawyer']['phone_reply_fee']) {
                $rules['phone_reply_fee'] = 'required';
            }
            $messages = array(
                'text_reply_fee.required' => '请填写图文咨询的资费',
                'phone_reply_fee.required' => '请填写电话咨询的资费',
            );

            $posts = Input::all();
            $validator = Validator::make ( $posts, $rules, $messages );
            if ($validator->fails()) {
                // the given data did not pass validation
                return $this->makeView(
                    'errors.400', 
                    $validator->errors(),
                    400
                );
            }
        
            // save
            try {
                $user = User::addOrUpdateLawyerFeeInfo($this->user->id, $posts);
            } catch(Exception $ex) {
                Log::error($ex);
                return $this->makeView(
                    'errors.503', 
                    '成为律师失败，请稍后再试。',
                    503
                );
            }
        } else {
            $user = $this->user;
        }

        $userArr = [];
        $userArr['id'] = $user['id'];
        $userArr['text_reply_fee'] = $user['lawyer']['text_reply_fee'];
        $userArr['phone_reply_fee'] = $user['lawyer']['phone_reply_fee'];

        return $this->makeView('center.becomelawyer.fee', $userArr, 200);
    }

    /**
     * work_scope
     * 
     * 提交成为律师需要的擅长领域信息
     */
    public function lawyerWorkScopeInfo() {
        $this->user->load('lawyer');
        $userArr = $this->user->toArray();

        if (Input::server ( 'REQUEST_METHOD' ) == 'POST') {
            $rules = [];

            if ($userArr['lawyer']['work_scope']) {
                $rules['work_scope'] = 'required|array';
            }
            $messages = array(
                'work_scope.required' => '请选择擅长的领域',
                'work_scope.array' => '擅长的领域要是数组',
            );
            $posts = Input::all();
            $validator = Validator::make ( $posts, $rules, $messages );
            if ($validator->fails()) {
                // the given data did not pass validation
                return $this->makeView(
                    'errors.400', 
                    $validator->errors(),
                    400
                );
            }

            try {
                $posts = Input::all();
                $user = User::addOrUpdateLawyerWorkscopeInfo($this->user->id, $posts);
            } catch(Exception $ex) {
                Log::error($ex);
                return $this->makeView(
                    'errors.503', 
                    '成为律师失败，请稍后再试。',
                    503
                );
            }
        } else {
            $user = $this->user;
        }
        
        $userArr = [];
        $userArr['id'] = $user['id'];
        $userArr['work_scope'] = UtilsHelper::getActualWorkScopes($user['lawyer']['work_scope']);

        return $this->makeView('center.becomelawyer.workscope', $userArr, 200);
    }

    /**
     * 提交成为律师需要的经历案例信息
     */
    public function lawyerExperienceInfo() {
        // introduce
        // experience
        // law_cases

        if (Input::server ( 'REQUEST_METHOD' ) == 'POST') {
            // save
            try {
                $posts = Input::all();
                $user = User::addOrUpdateLawyerExperienceInfo($this->user->id, $posts);
            } catch(Exception $ex) {
                Log::error($ex);
                return $this->makeView(
                    'errors.503', 
                    '成为律师失败，请稍后再试。',
                    503
                );
            }
        } else {
            $user = $this->user;
        }
        
        $userArr = [];
        $userArr['id'] = $user['id'];
        $userArr['introduce'] = $user['lawyer']['introduce'];
        $userArr['experience'] = $user['lawyer']['experience'];
        $userArr['law_cases'] = $user['lawyer']['law_cases'];

        return $this->makeView('center.becomelawyer.experience', $userArr, 200);
    }
}
